package test002;

import java.io.StringWriter;
import java.io.Writer;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.appengine.api.users.User;
//import com.google.gdata.util.AuthenticationException;
//import com.google.gdata.util.ServiceException;

//import java.io.IOException;
//import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;


public class MyContacts {



/**
 * This is a test template
 */

//    public String getMyContacts(String user, String paswd) {
    public String getMyContacts(User myUser, String sessionToken) {
    	Writer myWriter = new StringWriter();
      
      try {
        
        // Create a new Contacts service
        ContactsService myService = new ContactsService("My Application");
//        myService.setUserCredentials(user,paswd);
     // ### new auth begin

        myService.setAuthSubToken(sessionToken, null);
        
     // ### new auth end
        
        
        // Get a list of all entries
        URL metafeedUrl = new URL("http://www.google.com/m8/feeds/contacts/"+ myUser.getNickname() +"@gmail.com/base");
        myWriter.write("Getting Contacts entries...\n");
        ContactFeed resultFeed = myService.getFeed(metafeedUrl, ContactFeed.class);
        List<ContactEntry> entries = resultFeed.getEntries();
        for(int i=0; i<entries.size(); i++) {
          ContactEntry entry = entries.get(i);
          myWriter.write("\t" + entry.getTitle().getPlainText());
        }
        myWriter.write("\nTotal Entries: "+entries.size());
      }
      catch(Exception e) {
        e.printStackTrace();
      }

      return myWriter.toString();
    }
  }
